import Request, { AxiosInstance, AxiosResponse } from 'axios'
import { Notify } from 'vant'
const baseUrl = process.env.VUE_APP_COM_API
// const baseUrl = 'http://192.168.14.181:8080'
const request: AxiosInstance = Request.create({
  baseURL: baseUrl
})
export const fecth = (
  url: string,
  data: any = {},
  methods: 'get' | 'delete' | 'post' | 'put' = 'get',
  config?: object
) => {
  return new Promise<any>(resolve => {
    request[methods](url, data, {
      headers: {
        'xt-token': sessionStorage.getItem('token') || '',
        ...config
      }
    })
      .then((res: AxiosResponse) => {
        // console.log(res)
        if (res.status === 200) {
          resolve(res.data)
        } else {
          resolve(res.data)
          Notify({ type: 'warning', message: res.data.msg || '网络繁忙，请稍后重试' })
        }
      })
      .catch((err: any) => {
        console.log(err)
        Notify({ type: 'warning', message: (err && err.msg) || '网络繁忙，请稍后重试' })
      })
  })
}
